<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <!-- <el-row>
      <el-col :span="1">
        <span class="filter-item">角色名称:</span>
      </el-col>
      <el-col :span="4">
        <el-input v-model="search.name" />
      </el-col>
      <el-col :span="6">
        <span class="filter-item">状态:</span>
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      </el-col>
    </el-row> -->
    <el-form :inline="true" :model="listQuery" class="el_form hidden-xs-only">
      <el-form-item label="角色名称">
        <el-input v-model="listQuery.name" placeholder="角色名称" />
      </el-form-item>
      <el-form-item label="状 态">
        <el-select v-model="listQuery.status" placeholder="状 态" style="width: 120px;">
          <el-option label="全 部" value="" />
          <el-option label="启 用" value="0" />
          <el-option label="停 用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查 询</el-button>
      </el-form-item>
    </el-form>
    <el-form :inline="true" :model="listQuery" class="el_form2 hidden-sm-and-up">
      <el-form-item class="form_search">
        <el-button type="primary" icon="el-icon-search" class="form_search" circle plain @click="search"/>
      </el-form-item>
      <el-form-item label="" class="form_type">
        <el-select v-model="listQuery.status" placeholder="状 态">
          <el-option label="全 部" value="" />
          <el-option label="启 用" value="0" />
          <el-option label="停 用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="" class="form_name">
        <el-input v-model="listQuery.name" placeholder="角色名称" />
      </el-form-item>
      <div style="clear: both;" />
    </el-form>

    <!-- 查询结果 -->
    <div>
      <router-link to="/system/role/new"><el-button type="primary" style="float:right;margin-bottom:10px;">新 增</el-button></router-link>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="编号" prop="id" min-width="70"/>
      <el-table-column align="center" label="角色名称" prop="name" min-width="100"/>
      <el-table-column align="center" label="角色描述" prop="description" min-width="100"/>
      <el-table-column align="center" label="接收微信通知" prop="enableNotify" min-width="100">
        <template slot-scope="scope">
          <el-tag :type=" scope.row.enableNotify === 0 ? 'success' : 'danger' " size="medium">{{ scope.row.enableNotify === 0 ? '是' : '否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色状态" prop="status" min-width="85">
        <template slot-scope="scope">
          <el-tag :type=" scope.row.status === 0 ? 'success' : 'danger' " size="medium">{{ scope.row.status === 0 ? '启用' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :formatter="formatDate" align="center" label="创建时间" prop="createtime" min-width="160" />
      <el-table-column align="center" label="操作" min-width="230">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.name === '超级管理员'" size="mini" type="primary" @click="edit(scope.row.id)">编辑</el-button>
          <el-button :disabled="scope.row.name === '超级管理员'" :type=" scope.row.status === 0 ? 'danger' : 'success' " size="mini" @click="changeStatus(scope.row)">{{ scope.row.status === 0 ? '停用' : '启用' }}</el-button>
          <el-button type="info" size="mini" @click="desc(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { fetchList, changeStatus } from '@/api/role'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { getShopId } from '@/utils/auth'
import { MessageBox } from 'element-ui'

export default {
  name: 'Role',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        shopId: getShopId(),
        page: 1,
        limit: 20
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.getList()
    },
    formatDate(row, column, cellValue) {
      var t = new Date(row.createtime)
      return t.getFullYear() + '-' + (t.getMonth() + 1) + '-' + t.getDate() + ' ' + t.getHours() + ':' +
       t.getMinutes() + ':' + t.getSeconds()
    },
    formatStatus(row, column, cellValue) {
      return row.status === 0 ? '启用' : '停用'
    },
    edit(id) {
      this.$router.push({ path: '/system/role/edit', query: { id: id }})
    },
    desc(id) {
      this.$router.push({ path: '/system/role/desc', query: { id: id }})
    },
    changeStatus(row) {
      let operate = '启用'
      if (row.status === 0) {
        operate = '停用'
      }
      this.$confirm('是否' + operate + '角色："' + row.name + '"?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let status = 0
        if (row.status === 0) {
          status = 1
        }
        changeStatus(row.id, status).then(resp => {
          if (resp.data.code === '100') {
            this.$message({
              message: '成功',
              type: 'success'
            })
            if (row.status === 0) {
              row.status = 1
            } else {
              row.status = 0
            }
          } else {
            MessageBox.alert(resp.data.message, '错误', {
              confirmButtonText: '确定',
              type: 'error'
            })
          }
        })
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container{
  .el_form{
    max-width: 550px;
    margin: 0 auto -10px;
  }
  .el_form2{
    margin: 0 auto 10px;
    max-width: 400px;
    .form_name{
      min-width: 90px;
      width: 50%;
      float: right;
      margin: 0 5px 0 0;
    }
    .form_type{
      min-width: 80px;
      width: 30%;
      margin: 0;
      float: right;
    }
    .form_search{
      float: right;
      margin: 0 0 0 5px;
    }
  }
}
</style>
